/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package es.shinra.app.BBDD.Object.DAO;

import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import org.xmldb.api.base.*;
import org.xmldb.api.*;
/**
 *
 * @author aaron_oliva
 */
public class DataSource implements iDataSource{
    String eDriverExist;
    String eUriExist;
    String eDriverJDBC;
    String eUriJDBC;
    Collection col;
    Connection eConnection;

    public DataSource (){
        eDriverExist = "org.exist.xmldb.DatabaseImpl";
        eUriExist = "xmldb:exist://localhost:8080/exist/xmlrpc/db/App";
        eDriverJDBC = "com.mysql.jdbc.Driver";
        eUriJDBC = "jdbc:mysql://localhost/test";
        col = null;
        eConnection = null;
    }

    public Connection getConnection() {
        
        try {
            Class.forName(eDriverJDBC).newInstance();
            eConnection = (Connection) DriverManager.getConnection(eUriJDBC);

        } catch (Exception ex) {
            System.out.println("Problemas al hacer el getConnection JDBC--> "+ex.toString());
        }
        return eConnection;
    
    }

    public Connection getConnection(String eUserName, String ePassword) {
        
        try {
            Class.forName(eDriverJDBC).newInstance();
            eConnection = (Connection) DriverManager.getConnection(eUriJDBC,eUserName,ePassword);

        } catch (Exception ex) {
            System.out.println("Problemas al hacer el getConnection JDBC--> "+ex.toString());
        }
        return eConnection;
    }

    public Collection getCollection() {
        Class cl;
        try {
            cl = Class.forName(eDriverExist);
            Database database = (Database) cl.newInstance(); //Instancia de la BD
            database.setProperty("create-database", "true");
            DatabaseManager.registerDatabase(database); //Registrar la BD
            //Accedemos a la colección
            col = DatabaseManager.getCollection(eUriExist);
        } catch (Exception ex) {
            System.out.println("Problemas al hacer el getCollection de eXist--> "+ex.toString());
        }
        return col;

    }
    public Collection getCollection(String eUserName, String ePassword) {
        Class cl;
        try {
            cl = Class.forName(eDriverExist);
            Database database = (Database) cl.newInstance(); //Instancia de la BD
            database.setProperty("create-database", "true");
            DatabaseManager.registerDatabase(database); //Registrar la BD
            //Accedemos a la colección
            col = DatabaseManager.getCollection(eUriExist,eUserName,ePassword);
        } catch (Exception ex) {
            System.out.println("Problemas al hacer el getCollection de eXist--> "+ex.toString());
        }
        return col;
    }

}
